package io.renren.system.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.system.log.dao.SysLogErrorDao;
import io.renren.system.log.dto.SysLogErrorDTO;
import io.renren.system.log.entity.SysLogErrorEntity;
import io.renren.system.log.service.SysLogErrorService;
import io.renren.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 【日志管理-异常日志】
 */
@Service
public class SysLogErrorServiceImpl extends BaseServiceImpl<SysLogErrorDao, SysLogErrorEntity> implements SysLogErrorService {

    private QueryWrapper<SysLogErrorEntity> getWrapper(Map<String, Object> params) {
        String requestUri = StringUtils.checkObj2Str(params.get("requestUri"));
        String requestParams = StringUtils.checkObj2Str(params.get("requestParams"));
        String errorInfo = StringUtils.checkObj2Str(params.get("errorInfo"));
        String startDate = StringUtils.checkObj2Str(params.get("startDate"));
        String endDate = StringUtils.checkObj2Str(params.get("endDate"));

        if (StringUtils.isNotEmpty(startDate)) {
            startDate += " 00:00:00";
        }
        if (StringUtils.isNotEmpty(endDate)) {
            endDate += " 23:59:59";
        }

        QueryWrapper<SysLogErrorEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(requestUri), "request_uri", requestUri);
        wrapper.like(StringUtils.isNotEmpty(requestParams), "request_params", requestParams);
        wrapper.like(StringUtils.isNotEmpty(errorInfo), "error_info", errorInfo);
        wrapper.ge(StringUtils.isNotEmpty(startDate), "create_date", startDate);
        wrapper.le(StringUtils.isNotEmpty(endDate), "create_date", endDate);
        wrapper.orderByDesc(Constant.CREATE_DATE);
        return wrapper;
    }

    /**
     * 按查询条件分页获取日志列表数据
     *
     * @param params
     * @return
     */
    @Override
    public PageData<SysLogErrorDTO> page(Map<String, Object> params) {
        IPage<SysLogErrorEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, SysLogErrorDTO.class);
    }

    /**
     * 按查询条件获取日志列表数据
     *
     * @param params
     * @return
     */
    @Override
    public List<SysLogErrorDTO> list(Map<String, Object> params) {
        List<SysLogErrorEntity> entityList = baseDao.selectList(getWrapper(params));
        return ConvertUtils.sourceToTarget(entityList, SysLogErrorDTO.class);
    }

    /**
     * 新增保存异常日志
     *
     * @param entity
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysLogErrorEntity entity) {
        insert(entity);
    }

}